import 'dart:convert';
import 'dart:math';
import 'dart:ui';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';

double getStatusBarHeight() {
  return MediaQueryData.fromWindow(window).padding.top;
}

/*
  * Base64加密
  */
String encodeBase64(String data) {
  var content = utf8.encode(data);
  var digest = base64Encode(content);
  return digest;
}

String getRandomString(int length) {
  String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  Random random = new Random();
  StringBuffer sb = new StringBuffer();
  for (int i = 0; i < length; i++) {
    int number = random.nextInt(62);
    sb.write(str.substring(number, number + 1));
  }
  return sb.toString();
}

/*
  * Base64解密
  */
String decodeBase64(String data) {
  return String.fromCharCodes(base64Decode(data));
}

// getLoginResponse(Function(LoginResponse response) success) {
//   String json = SpUtil.getString('login_response', defValue: '');
//   if (json.length == 0) {
//     success(null);
//   }
//   // ignore: missing_return
//   JsonUtil.getObj<LoginResponse>(json, (v) {
//     LoginResponse a = JsonConvert.fromJsonAsT<LoginResponse>(v);
//     success(a);
//   });
// }
//
// putLoginData(LoginResponse data) {
//   if (data == null) {
//     SpUtil.putString('login_response', '');
//     SpUtil.putBool(Constants.IS_REALNAME, false);
//   } else {
//     SpUtil.putString('login_response', JsonUtil.encodeObj(data));
//   }
// }

extension StringPhone on String {
  String stringPhone() {
    if (this.isEmpty) {
      return "";
    }
    String addString = " ";
    var str = this.replaceAll(" ", "");
    if (str.length > 3) {
      String first = str.substring(0, 3);
      String second = str.substring(3);
      if (second.length > 4) {
        String third = second.substring(0, 4);
        String forth = second.substring(4);
        return first + addString + third + addString + forth;
      } else {
        return first + addString + second;
      }
    } else {
      return str;
    }
  }
}

//扩展String，增加字符串分隔方法
extension StringSeprate on String {
  String stringSeparate(
      {int count = 3, String separator = ",", bool fromRightToLeft = false}) {
    if (this.isEmpty) {
      return "";
    }

    if (count < 1) {
      return this;
    }

    if (count >= this.length) {
      return this;
    }

    var str = this.replaceAll(separator, "");

    var chars = str.runes.toList();
    var namOfSeparation =
        (chars.length.toDouble() / count.toDouble()).ceil() - 1;
    var separatedChars = List(chars.length + namOfSeparation.round());
    var j = 0;
    for (var i = 0; i < chars.length; i++) {
      separatedChars[j] = String.fromCharCode(chars[i]);
      print(i.toString());

      if (i > 0 && (i + 1) < chars.length && (i + 1) % count == 0) {
        j += 1;
        separatedChars[j] = separator;
      }

      j += 1;
    }

    return fromRightToLeft
        ? String.fromCharCodes(separatedChars.join().runes.toList().reversed)
        : separatedChars.join();
  }
}
